<?php

class Auth extends Admin_Controller{
	function __construct(){
		parent::__construct();
		$this->load->model('AuthManager/Auth_model','auth');
		// $this->shop_token = $_SESSION['admin_info']['shop_token'];
	}
	
	function index(){
		$list = $this->auth->getAuthList();
		$data['list'] = $list;
		$this->load->view('Setting/authority',$data);
	}

	function addAuth($id=0){
		if($_POST){
            $post = $this->input->post();
            // print_r($post);exit();
            // $this->activity->addActivity($post);
            $return = $this->auth->handleAddAuth($post);
            $this->handleResult($return);
        }else{
            $data = array();
            if($id!=0){
                $auth = $this->auth->getAuthById($id);
                $auth['authlist'] = json_decode($auth['authlist'],true);
                $data['auth'] = $auth;
            }
            //获取模块列表
            $model_list = $this->auth->getModuleList();
            $data['module'] = $model_list;
            // print_r($data);exit();
            $this->load->view('Setting/addRole',$data);
        }
	}

	 /* 冻结
	 * @DateTime 2017-07-17
	 * @author zyl
	 * @return   [type]     [description]
	 */
	function freeze(){
		$post = $this->input->post();

		$data['status'] = 0;
		$where = $post['ids'];

		$return = $this->auth->updateAuth_in($data,$where);

		$this->handleResult($return);
	}
	/**
	 * 解冻
	 * @DateTime 2017-07-17
	 * @author zyl
	 * @return   [type]     [description]
	 */
	function fraw(){
		$post = $this->input->post();

		$data['status'] = 1;
		$where = $post['ids'];

		$return = $this->auth->updateAuth_in($data,$where);

		$this->handleResult($return);
	}

	/*
	权限组
	www.zsgy.com/AuthManager/Auth/authgroup
	*/
	function authgroup(){
		//获取模块列表
		
			         
		if (!$this->authcheck('auth','select')) {
			$this->error('您无权限访问该模块',base_url());
			return;
		}
		
		$data['authlist'] = $this->auth->getAuthList($this->shop_token);
		//取出权限名称列表
		foreach ($data['authlist'] as $key => $value) {
			//所有需验证权限的权限名称 比如一个模块叫做教育  那么教育是模块名，请求教育权限的时候的权限名就是数据库中对应教育这个modulename的 authname
			
			$tmpdata[] = $value['authname'];

		}
		
		//获取权限组列表
		
		$data['authgroup'] = $this->auth->getAuthGroupList($this->shop_token);
		//处理权限组对每个模块的权限详情
		foreach ($data['authgroup']  as $key => $value) {
			//每个权限组的权限详情
		
			$tmpauthlist = json_decode($value['authlist'],true);
			$arraykeys = array_keys($tmpauthlist);
			
			foreach ($tmpdata as $k => $v) {
				
				if(in_array($v, $arraykeys)){
					$data['authgroup'][$key][$v]='';
					foreach ($tmpauthlist[$v] as $key1 => $value1) {
						switch ($value1) {
							case 'add':
								$data['authgroup'][$key][$v] = $data['authgroup'][$key][$v].'新增,';
								break;
							case 'del':
								$data['authgroup'][$key][$v] =$data['authgroup'][$key][$v].'删除，';
								break;
							case 'edit':
								$data['authgroup'][$key][$v] =$data['authgroup'][$key][$v].'编辑，';
								break;
							case 'select':
								$data['authgroup'][$key][$v] =$data['authgroup'][$key][$v].'查看，';
								break;
							case 'reviewed':
								$data['authgroup'][$key][$v] =$data['authgroup'][$key][$v].'审核，';
								break;
							case 'all':
								$data['authgroup'][$key][$v] =$data['authgroup'][$key][$v].'查看全部，';
								break;
							
							default:
								# code...
								break;
						}
					}
					
				}else{
					$data['authgroup'][$key][$v] = "无";
				}
				
			}
		}
		$data['arraykeys'] = $tmpdata;
		
		$this->load->view('AuthManager/authgroup',$data);
	}

	/*
	管理员列表
	www.zsgy.com/AuthManager/Auth/managerlist
	*/
	function managerlist($id=0){
		if (!$this->authcheck('auth','select')) {
			$this->error('您无权限访问该模块',base_url());
			return;
		}
		$res=$this->auth->getmanagerlist($this->shop_token,$id);

		if($res['code']){
			$data['list'] = $res['data'];
			$this->load->view('AuthManager/managerlist',$data);
		}else{
			$this->handleResult($res,base_url().'AuthManager/Auth/authgroup');
		}
		
	}
	/*
	添加管理员
	www.zsgy.com/AuthManager/Auth/addmanager
	*/
	function addmanager()
	{
		if (IS_POST){
			$post = $this->input->post();
			$post = Common::_clean($post);
			$result = $this->auth->addUseradmin($this->shop_token,$post);
			$this->handleResult($result);
		}else{
			$data['list'] = $this->auth->getAuthGroup($this->shop_token);
			$this->load->view('AuthManager/addmanager',$data);
		}

		
	}
	/*
	添加用户权限组
	www.zsgy.com/AuthManager/Auth/addauthgroup
	$post
	*/
	function addauthgroup(){
		//获取平台需验证模块列表
		$data['list'] = $this->auth->getAuthList($this->shop_token);

		if (IS_POST){
			$post = $this->input->post();
			$post = Common::_clean($post);
			$list = array();
			foreach ($data['list'] as $key => $value) {
				//获取权限模块名
				array_push($list, $value['authname']);
			}
			//将post数据权限值进行转换
			foreach ($post as $key => $value) {
				if(in_array( $key,$list )){
					foreach ($value as $k => $v) {
						switch ($v) {
							case '1':
								$post[$key][$k]='add';
								break;
							case '2':
								$post[$key][$k]='del';
								break;
							case '3':
								$post[$key][$k]='edit';
								break;
							case '4':
								$post[$key][$k]='select';
								break;
							case '5':
								$post[$key][$k]='reviewed';
								break;
							case '6':
								$post[$key][$k]='all';
								break;
							
							default:
								# code...
								break;
						}
					}
				}
			}
			$result = $this->auth->addAuthGroup($this->shop_token,$post);
			$this->handleResult($result);
		}else{
			
			$this->load->view('AuthManager/addauthgroup',$data);
		}
		
	}
	/*
	权限测试
	www.zsgy.com/AuthManager/Auth/test
	$post
	*/
	function test(){
		$this->authcheck('Opt','edit','23');
	}

}